JavaScript
is a single-threaded language. If a time-consuming operation is performed on the main thread of Js
, not only will the asynchronous event callback fail to complete normally, but the browser's rendering thread will also be blocked, preventing the page from being rendered properly. Web Worker
allows JavaScript calculations to be delegated to background threads, which can perform tasks without interfering with the user interface.
A worker
is an object created using a constructor function to run a Js
file. This Js
file contains the code that will run in the worker
thread. The global object running in the worker
is not the current window
. The global object for the dedicated worker
thread environment is DedicatedWorkerGlobalScope
, and the global object for the shared worker
thread environment is SharedWorkerGlobalScope
.
In a worker
, you can run any JavaScript code, but you cannot directly manipulate DOM
nodes or use the default methods and properties of the window
object. However, many methods under the window
object, including WebSockets
and IndexedDB
, are implemented in the worker
global object.
Communication between the worker
thread and the main thread is done through sending messages using postMessage
and receiving messages using the onmessage
event handler. During this process, the data is not shared but copied.
As long as it is running in the same origin parent page, a worker
can generate new workers
in sequence. In addition, a worker
can also use XMLHttpRequest
for network I/O, but the responseXML
and channel
properties of XMLHttpRequest
will always return null
.
A dedicated worker
can only be used by the script that generated it. It is generated using a constructor function, and data is passed to the worker
thread through a message passing mechanism. After the worker
thread finishes the calculation, the data is passed back for further operations. The worker
thread can be closed in either the main thread or the worker
thread.
A shared worker
can be used by multiple scripts at the same time, even if these scripts are being accessed by different window
, iframe
, or worker
instances. This means that a shared worker
can be used for communication between multiple browser windows. However, communication with a shared worker
must be within the same origin and cannot be cross-domain. Generating a shared worker
is very similar to generating a dedicated worker
, except that the constructor name is different. One major difference between them is that a shared worker
must be communicated with through a specific open port object, which is used by the script to communicate with the worker
. In a dedicated worker
, this part is implicit. If both the parent thread and the worker
thread need bidirectional communication, they both need to call the start()
method. Message passing still uses postMessage
, but it must be implemented through the postMessage
method on the port.